package com.all.play.myeventbus;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.all.play.myeventbus.bean.MessageEvent;
import com.all.play.myeventbus.bean.StickyEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.bta)
    Button bta;
    @BindView(R.id.btb)
    Button btb;
    @BindView(R.id.tv)
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {
        tv.setText("EventBus");

        EventBus.getDefault().register(MainActivity.this);

    }

    @OnClick({R.id.bta, R.id.btb})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bta:
                startActivity(new Intent(MainActivity.this,SecondActivity.class));
                break;
            case R.id.btb:
                //发送粘性事件
                EventBus.getDefault().postSticky(new StickyEvent("JInMao"));
                startActivity(new Intent(MainActivity.this,SecondActivity.class));

                break;
        }
    }


    /*
    * 接收消息
    * */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBus(MessageEvent me){
        tv.setText(me.name);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(MainActivity.this);
    }
}
